﻿using System;

namespace Ratatoskr
{
    public class RepeaterComponent : RepeaterComponent<Component>
    {
        public RepeaterComponent(string type, string repeatType, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(type, repeatType, open, priority, enabled, single)
        {
        }

        public RepeaterComponent(string type, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(type, open, priority, enabled, single)
        {
        }
    }

    public class RepeaterComponent<T> : ServerComponent<T> where T : class
    {
        protected readonly string _repeat;
        public string RepeaterType { get { return _repeat; } }

        protected float _priority;
        public float RepeaterPriority { get { return _priority; } }

        protected bool _enabled;
        public bool RepeaterEnabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        public RepeaterComponent(string type, string repeatType, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : base(type, open, single)
        {
            if (repeatType == null)
                throw new ArgumentNullException("Cannot create a repeater with a null repeat type.");
            else _repeat = repeatType;
            _enabled = enabled;
            _priority = priority;
            AddServerProfile(_repeat, Repeat, priority);
        }

        public RepeaterComponent(string type, bool open = false, float priority = 0, bool enabled = true, bool single = false)
            : this(type, type, open, priority, enabled, single)
        {
        }

        protected virtual void Repeat(object sender, ServerComponentEventArgs e)
        {
            if (_enabled && ExtendRepeatConditions())
                DoTrigger(e.Args);
        }

        public void SetRepeaterPriority(int priority)
        {
            SetServerPriority(_repeat, priority);
        }

        protected virtual bool ExtendRepeatConditions()
        {
            return true;
        }
    }
}